package com.ruoyi.demo.controller.User;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.domain.vo.UserMeetInfoVO;
import com.ruoyi.demo.service.IDeviceLogService;
import com.ruoyi.demo.service.IMyMeetService;
import com.ruoyi.demo.util.DeviceLogInsertUtil;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.service.IDeviceService;
import com.ruoyi.system.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 我的会议管理
 * @author ruoyi
 */
@RestController
@RequestMapping("/demo/meet")
public class MyMeetController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IDeviceLogService deviceLogService;

    @Autowired
    private IMyMeetService IMyMeetService;

    /**
     * 获取会议列表
     */
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required = false) String theme,
            @RequestParam(required = false) String roomAddress,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String requestStatus,
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        startPage();
        List<UserMeetInfoVO> list = IMyMeetService.listMeetings(theme, roomAddress, status, requestStatus, userId);

        // 打印日志，检查返回数据
        System.out.println("返回数据：" + list);

        return getDataTable(list);
    }

    @GetMapping("/myMeetByUserId")
    public TableDataInfo myMeetByUserId(
            @RequestParam(required = false) String theme,
            @RequestParam(required = false) String roomAddress,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String requestStatus,
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        startPage();
        List<UserMeetInfoVO> list = IMyMeetService.myMeetList(theme, roomAddress, status, requestStatus, userId);

        // 打印日志，检查返回数据
        System.out.println("返回数据：" + list);

        return getDataTable(list);
    }

    /**
     * 获取会议室设备列表
     */
    @GetMapping("/devices/{meetId}/{userId}/{roomId}")
    public AjaxResult getMeetingDevices(@PathVariable("meetId") Long meetId,
                                      @PathVariable("userId") Long userId,
                                      @PathVariable("roomId") Long roomId) {
        try {
            List<Device> devices = deviceService.selectDeviceListByRoomId(roomId);
            return AjaxResult.success(devices);
        } catch (Exception e) {
            return AjaxResult.error("获取设备列表失败：" + e.getMessage());
        }
    }

    /**
     * 更新设备状态
     */
    @Log(title = "更新设备状态", businessType = BusinessType.UPDATE)
    @PutMapping("/device/update")
    public AjaxResult updateDeviceStatus(@RequestBody Device device) {
        try {
            // 获取设备原状态
            Device oldDevice = deviceService.selectDeviceByDeviceId(device.getDeviceId());
            if (oldDevice == null) {
                return AjaxResult.error("设备不存在");
            }

            // 更新设备状态
            int rows = deviceService.updateDevice(device);
            if (rows > 0) {
                // 记录设备操作日志
                DeviceLogInsertUtil.logDeviceChanges(oldDevice, device, deviceLogService, roomService);
                return AjaxResult.success();
            }
            return AjaxResult.error("更新设备状态失败");
        } catch (Exception e) {
            return AjaxResult.error("更新设备状态失败：" + e.getMessage());
        }
    }

    /**
     * 删除会议
     */
    @DeleteMapping("/{meetIds}")
    public AjaxResult remove(@PathVariable Long[] meetIds) {
        try {
            IMyMeetService.deleteMeetings(meetIds);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("删除会议失败");
        }
    }

    /**
     * 获取预定会议列表
     */
}